//
//  AccountSafeMenuController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/15.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "AccountSafeMenuController.h"

#import "AssetPasswordController.h"
#import "ChangePswViewController.h"
#import "BindInfoViewController.h"
#import "SimpleInfoTabCell.h"
#import "CYXBaseRequest.h"

@interface AccountSafeMenuController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/// <#注释#>
@property (nonatomic, strong) NSArray *safeMenuList;
@end

@implementation AccountSafeMenuController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configMenuInfo];
    
    [LCNotifiCenter addObserver:self selector:@selector(refreshView) name:@"UserInfoUpdated" object:nil];
}

- (void)setupUI{
    [super setupUI];
    self.title = LCLocalString(@"account_security");
    [self.tableView registerNib:[UINib nibWithNibName:@"SimpleInfoTabCell" bundle:nil] forCellReuseIdentifier:@"SimpleInfoTabCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)refreshView{
    [self configMenuInfo];
    [self.tableView reloadData];
}

- (void)configMenuInfo{
    UserInfoModel *user = kAppDelegate.curUser;
    if (!user) {
        self.safeMenuList = @[];
        return;
    }
    NSArray *safeMenuList = @[
        @{@"title":@"HID",@"subTitle":user.hid,@"isHideArr":@(YES)},
        @{@"title":LCLocalString(@"phone_num"),@"subTitle":user.phone,@"isHideArr":@(NO)},
        //        @{@"title":@"邮箱",@"subTitle":@"1999999@qq.com",@"isHideArr":@(NO)},
        @{@"title":LCLocalString(@"psw_xgmm"),@"subTitle":@"",@"isHideArr":@(NO)},
        @{@"title":LCLocalString(@"psw_zjmm"),@"subTitle":@"",@"isHideArr":@(NO)}
    ];
    
    self.safeMenuList = safeMenuList;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.safeMenuList.count) {
        return 0;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.safeMenuList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *safeMenuM = self.safeMenuList[indexPath.row];
    SimpleInfoTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleInfoTabCell"];
    NSString *title = safeMenuM[@"title"];
    cell.title.text = title;
    cell.subTitle.text = safeMenuM[@"subTitle"];
    BOOL isHID = [title isEqualToString:@"HID"];
    cell.subTitle.textColor = isHID?TextColor_929acc:[UIColor whiteColor];
    cell.moreArr.hidden = isHID;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return;
            break;
        case 1:
        {
            BindInfoViewController *vc = LoadVCFormXib(BindInfoViewController);
            vc.bindInfoType = 0;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            //        case 2:
            //        {
            //            BindInfoViewController *vc = LoadVCFormXib(BindInfoViewController);
            //            vc.bindInfoType = 1;
            //            [self.navigationController pushViewController:vc animated:YES];
            //
            //        }
            break;
        case 2:
        {
            ChangePswViewController *vc = LoadVCFormXib(ChangePswViewController);
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            AssetPasswordController *vc = LoadVCFormXib(AssetPasswordController);
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (IBAction)logoutAction:(id)sender {
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认退出登录？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:LCLocalString(@"cancel") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //取消处理
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:LCLocalString(@"confirm") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self logOut_net];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}

- (void)logOut_net{
    
    
    [CYXBaseRequest getWithUrl:API_logout param:nil resultClass:nil success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            if([res boolValue]){
                LCPostNotification(@"UserLogOutSuccess");
                [kAppDelegate initTabbarVC];
            }
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
}

@end

